package com.douing.ad.dsp.bidding.rtb;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.douing.ad.dsp.bidding.dto.AdRequestDto;
import com.douing.ad.dsp.bidding.dto.MatchedCreativeDto;
import com.douing.ad.dsp.bidding.tj.utils.NodeData;
import com.douing.ad.dsp.bidding.utils.RedisUtils;

public class PriceMatch {

    public static MatchedCreativeDto PriceMatchImp(AdRequestDto request, List<MatchedCreativeDto> matchedCreatives, NodeData nodeData,Map<String,NodeData> nodeDataMap) {
        if (matchedCreatives == null || matchedCreatives.size() == 0) {
            return null;
        }
        long bidPrice = GetBidPrice(request.getImp().getBidfloor());
        List<MatchedCreativeDto> finalMatched = new ArrayList<MatchedCreativeDto>();

        for (int i = 0; i < matchedCreatives.size(); i++) {
            MatchedCreativeDto matched = matchedCreatives.get(i);
            if (bidPrice < matched.getMaxPrice()) {
                NodeData nd = nodeDataMap.get(matched.getId()+"");
                if (bidPrice == 0) {
                    Common.WriteLog("bidfloor error,biding by user price,request id is "+ request.getRequestId()+";impid is "+matched.getImpid());
                    BigDecimal bid = new BigDecimal(String.valueOf(matched.getMaxPrice()));
                    bidPrice = bid.multiply(new BigDecimal("" + Common.configInfo.getRationobidfloor())).longValue();
                }
                //zbj访客增加出价
//                if(!("").equals(matched.getCrowds())){
//                    String crowd = RedisUtils.get("zbj_"+matched.getUid());
//                    if(crowd != null){
//                        nd.setStep8(1);
//                        bidPrice = (new BigDecimal(String.valueOf(bidPrice))).multiply(new BigDecimal("" + Common.configInfo.getBidcrowd())).longValue();
//                        Common.WriteLog("zbj cookie is matched ,advertiser id is "+ matched.getCreateUserId());
//                    }
//                }
                matched.setBidPrice(bidPrice);
                finalMatched.add(matched);
                Common.WriteLog("bidprice is "+bidPrice +";request id is "+ request.getRequestId()+";impid is "+matched.getImpid());
                // TODO
                nd.setStep6(1);  
            }else{
                Common.WriteLog("maxprice is " + matched.getMaxPrice() + ",bidprice is" + bidPrice+";advertiser id is "+ matched.getCreateUserId());
            }
        }
        if (finalMatched.size() == 0) {
            Common.WriteLog("price match false,bidfloor is " + request.getImp().getBidfloor() + ",bidprice is" + bidPrice+";request id is "+ request.getRequestId());
            return null;
        }
        // TODO
        for (Map.Entry<String,NodeData> entry : nodeDataMap.entrySet()) {  
            entry.getValue().setStep7(1);
        } 
        
        if (finalMatched.size() == 1) {
            return finalMatched.get(0);
        }
        Random random = new Random();
        return finalMatched.get(random.nextInt(finalMatched.size()));
    }

    public static List<MatchedCreativeDto> AppPriceMatchImp(Map<String, List<MatchedCreativeDto>> matchedCreatives, NodeData nodeData,Map<String,NodeData> nodeDataMap) {
        if (matchedCreatives == null || matchedCreatives.size() == 0) {
            return null;
        }
        List<MatchedCreativeDto> retPriceMatchs = new ArrayList<MatchedCreativeDto>();
        for (String impid: matchedCreatives.keySet()) {
            List<MatchedCreativeDto> impMatchList = matchedCreatives.get(impid);
            List<MatchedCreativeDto> finalMatched = new ArrayList<MatchedCreativeDto>();
            
            for (int i = 0; i < impMatchList.size(); i++) {
                MatchedCreativeDto m = impMatchList.get(i);
                NodeData nd = nodeDataMap.get(m.getId()+"");
                if(m.getBidfloor() < 0){
                    continue;
                }
                long bidPrice = GetBidPrice(m.getBidfloor());
                if (bidPrice < m.getMaxPrice()) {
                    if (bidPrice == 0) {
                        Common.WriteLog("bidfloor error,biding by user price;impid is "+m.getImpid());
                        BigDecimal bid = new BigDecimal(String.valueOf(m.getMaxPrice()));
                        bidPrice = bid.multiply(new BigDecimal("" + Common.configInfo.getRationobidfloor())).longValue();
                    }
                    m.setBidPrice(bidPrice);
                    finalMatched.add(m);
                    Common.WriteLog("bidprice is "+bidPrice +";impid is "+m.getImpid());
                    // TODO
                    nd.setStep6(1);
                }else{
                    Common.WriteLog("price match false,bidfloor is " + m.getBidfloor() + ",bidprice is" + bidPrice + ";advertiserid is "+m.getCreateUserId()); 
                }
                 
                if (finalMatched.size() == 0) {
                    continue;
                }
                if (finalMatched.size() == 1) {
                    // TODO
                    nd.setStep7(1);
                    retPriceMatchs.add(finalMatched.get(0));
                    continue;
                }
                Random random = new Random();
                retPriceMatchs.add(finalMatched.get(random.nextInt(finalMatched.size())));
                
            }

        }
        return retPriceMatchs;
    }

    public static long GetBidPrice(long bidfloor) {
        long ret = 0;
        if (bidfloor <= 0) {
            return ret;
        }

        BigDecimal bid = new BigDecimal(String.valueOf(bidfloor));
        BigDecimal db = bid.multiply(new BigDecimal("" + Common.configInfo.getBidratio()));
        return db.longValue();
    }

}
